//node.js写一个简单的服务器
const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
    //获取请求的路径
    const path = req.url;
    //判断请求的路径
    if (path === '/') {
        //返回public目录下的index.html文件的内容
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write(fs.readFileSync('public/index.html'));
        res.end();
    } else if (path === '/addUser') {
        //获取post请求的参数，以json格式返回
        let body = '';
        req.on('data', chunk => {
            body += chunk.toString();
        });
        req.on('end', () => {
            console.log(body);
        });
    } else {
        res.writeHead(404, { 'Content-Type': 'text/html' });
    }
}).listen(3000, '127.0.0.1', () => {
  console.log('Server running at http://127.0.0.1:3000/');
})